1   /*
2    * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 6275329
27   * @summary lazySet methods
28   */
29  
30  import java.util.concurrent.atomic.*;
31  import java.util.*;
32  
33  public class Lazy {
34      volatile int ii;
35      volatile long ll;
36      volatile Boolean bb;
37      static final Lazy z = new Lazy();
38  
39      public static void main(String[] args) throws Exception {
40          final AtomicBoolean b = new AtomicBoolean();
41          final AtomicInteger i = new AtomicInteger();
42          final AtomicLong    l = new AtomicLong();
43          final AtomicReference<Long> r = new AtomicReference<Long>();
44  
45          final AtomicIntegerArray ia = new AtomicIntegerArray(1);
46          final AtomicLongArray    la = new AtomicLongArray(1);
47          final AtomicReferenceArray<Long> ra = new AtomicReferenceArray<Long>(1);
48  
49          final AtomicIntegerFieldUpdater<Lazy> iu =
50              AtomicIntegerFieldUpdater.newUpdater(Lazy.class, "ii");
51          final AtomicLongFieldUpdater<Lazy> lu =
52              AtomicLongFieldUpdater.newUpdater(Lazy.class, "ll");
53          final AtomicReferenceFieldUpdater<Lazy,Boolean> ru =
54              AtomicReferenceFieldUpdater.newUpdater(Lazy.class,
55                                                     Boolean.class, "bb");
56  
57          Thread[] threads = {
58              new Thread() { public void run() { b.lazySet(true);    }},
59              new Thread() { public void run() { i.lazySet(2);       }},
60              new Thread() { public void run() { l.lazySet(3L);      }},
61              new Thread() { public void run() { r.lazySet(9L);      }},
62              new Thread() { public void run() { ia.lazySet(0,4);    }},
63              new Thread() { public void run() { la.lazySet(0,5L);   }},
64              new Thread() { public void run() { ra.lazySet(0,6L);   }},
65              new Thread() { public void run() { iu.lazySet(z,7);    }},
66              new Thread() { public void run() { lu.lazySet(z,8L);   }},
67              new Thread() { public void run() { ru.lazySet(z,true); }}};
68  
69          for (Thread t : threads) t.start();
70          for (Thread t : threads) t.join();
71  
72          if (! (b.get()   == true &&
73                 i.get()   == 2    &&
74                 l.get()   == 3L   &&
75                 r.get()   == 9L   &&
76                 ia.get(0) == 4    &&
77                 la.get(0) == 5L   &&
78                 ra.get(0) == 6L   &&
79                 z.ii      == 7    &&
80                 z.ll      == 8L   &&
81                 z.bb      == true))
82              throw new Exception("lazySet failed");
83      }
84  }